package com.hbx.residential_service.controller;

import com.github.pagehelper.PageInfo;
import com.hbx.residential_service.domain.CarSite;
import com.hbx.residential_service.service.CarSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/carsite")
public class CarSiteController {
    @Autowired
    private CarSiteService carSiteService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(Model model, HttpSession session, @RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<CarSite> cs = carSiteService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(cs);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("carsite-list");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }

    @RequestMapping("/add.do")
    public String addCarSite(CarSite carSite)throws Exception{
        carSiteService.addCarSite(carSite);
        return "redirect:findAll.do";
    }

    @RequestMapping("delete.do")
    public String deleteCarSite(String csIds)throws Exception{
        String[] strs = csIds.split(",");
        for (int i = 0; i < strs.length; i++) {
            try {
                carSiteService.deleteCarSite(Integer.parseInt(strs[i]));
            } catch (Exception e) {
            }
        }
        return "redirect:findAll.do";
    }

    @RequestMapping("/findupdbyid.do")
    public ModelAndView findUpdCSById(int csId){
        CarSite carSite =carSiteService.findUpdCSById(csId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("carSite",carSite);
        mv.setViewName("carsite-update");
        return mv;
    }

    @RequestMapping("/updbyid.do")
    public String UpdateCSById(CarSite carSite){
        carSiteService.UpdateCSById(carSite);
        return "redirect:findAll.do";
    }

    @RequestMapping("/fuzzysearchbyusername.do")
    public ModelAndView fsByUsername(Model model,HttpSession session,String content,@RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size)throws Exception{
        ModelAndView mv = new ModelAndView();
        content = new String(content.getBytes("iso-8859-1"),"utf-8");
        List<CarSite> fscs = carSiteService.fsByUsername(content,page,size);
        PageInfo pageInfo = new PageInfo(fscs);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("content",content);
        mv.setViewName("carsite-search");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }
}
